﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Gao.Poker.App.ViewModel;

namespace Gao.Poker.App.Game
{
    /// <summary>
    /// Interaction logic for PlayGame.xaml
    /// </summary>
    public partial class PlayGame : Window
    {
        private PlayGameViewModel _viewModel;
        private Int32 _competitionID;

        private Boolean isLoad = false;

        private Boolean isRest = false;

        public Int32 CompetitionID
        {
            get { return _competitionID; }
            set
            {
                _competitionID = value;
                _viewModel.CompetitionID = _competitionID;
            }
        }

       

        public PlayGame()
        {
            InitializeComponent();

            Loaded += PlayGame_Loaded;

            _viewModel = new PlayGameViewModel();
            DataContext = _viewModel;
            _viewModel.CurrentLevel = 1;

            shade.Visibility = Visibility.Hidden;
            RestTime.Visibility = Visibility.Hidden;
        }

        private void PlayGame_Loaded(object sender, RoutedEventArgs e)
        {
            CurrentLevelTime.TimeClear += CurrentLevelTime_TimeClear;
            NextBreakTime.TimeClear += NextBreakTime_TimeClear;
            RestTime.TimeClear += RestTime_TimeClear;
            ObjectToView();

            bdrCaption.Width = Width*0.7;

            isLoad = true;
            SetCaption(tbCaption.Text);
        }

        public Grid GetPlayInfo()
        {
            return gameInfo;
        }

        public void Pause()
        {
            CurrentLevelTime.Pause();
            NextBreakTime.Pause();
            
            shade.Visibility = Visibility.Visible;
            shadeInfo.Text = "比赛暂停";
        }

        public void Continue()
        {
            CurrentLevelTime.Continue();
            NextBreakTime.Continue();
            shade.Visibility = Visibility.Hidden;
        }

        private void ObjectToView()
        {
            var competitionLevel = _viewModel.CompetitionLevel;
            if (competitionLevel == null)
            {
                shade.Visibility = Visibility.Visible;
                shadeInfo.Text = "比赛结束";
                return;
            }
            CurrentLevelTime.Minute = competitionLevel.CompetitionTime;
            CurrentLevelTime.Refresh();

            CurrentLevelDescribe.Text = String.Format("Blind {0}", competitionLevel.Blind);
            CurrentLevelAnte.Text = String.Format("Ante {0}", competitionLevel.AppendGamble);

            var list = _viewModel.Competition.CompetitionLevels.Where(a => a.Level > competitionLevel.Level).ToList();
            list.Sort();
            int breakRestTime = competitionLevel.CompetitionTime;
            isRest = false;
            Boolean isNextCompetion = true;
            foreach (var level in list)
            {
                if(level.AfterRestTime > 0)
                {
                    if (isNextCompetion) isRest = true;
                    RestTime.Minute = level.AfterRestTime;
                    break;
                }
                else
                {
                    isNextCompetion = false;
                    breakRestTime += level.CompetitionTime;
                }
            }

            NextBreakTime.Minute = breakRestTime;
            NextBreakTime.Refresh();


            //CompetitionLevel level = null;


            //for (int i = 0; i < Competition.CompetitionLevels.Count; i++)
            //{
            //    var cl = Competition.CompetitionLevels[i];
            //    if (competitionLevel + 1 == cl.Level)
            //    {
            //        level = cl;
            //    }
            //}
            //return level;

            //var nextCompetitionLevel = NextCompetitionLevel;
            //if (nextCompetitionLevel != null)
            //{
            //    NextLevelTime.Minute = nextCompetitionLevel.AfterRestTime + competitionLevel.CompetitionTime;
            //    CurrentLevelTime.Refresh();

            //    NextLevelDescribe.Text = nextCompetitionLevel.Blind;
            //    AppendGamble.Text = String.Format("附加前注：{0}", nextCompetitionLevel.AppendGamble);
            //}
            //else
            //{
            //    NextLevelTime.Minute = competitionLevel.CompetitionTime;
            //}
        }

        /// <summary>
        /// 休息
        /// </summary>
        void RestTime_TimeClear()
        {
            shade.Visibility = Visibility.Hidden;
            shadeInfo.Text = "比赛暂停";
            RestTime.Visibility = Visibility.Hidden;
            ObjectToView();
        }

        /// <summary>
        /// 下一局
        /// </summary>
        private void NextBreakTime_TimeClear()
        {
            if (isRest)
            {
                _viewModel.CurrentLevel += 1;
                shade.Visibility = Visibility.Visible;
                shadeInfo.Text = "比赛休息中";
                RestTime.Visibility = Visibility.Visible;
                RestTime.Refresh();
            }
        }
        /// <summary>
        /// 当前比赛
        /// </summary>
        void CurrentLevelTime_TimeClear()
        {
            
            if(!isRest)
            {
                _viewModel.CurrentLevel += 1;
                ObjectToView();
            }
        }

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }

        private void closeButton_Click(object sender, RoutedEventArgs e)
        {
            Visibility = Visibility.Hidden;
            Pause();
        }

        public void MaxWindow()
        {
            WindowState = WindowState.Maximized;
        }

        private void maxButton_Click(object sender, RoutedEventArgs e)
        {
            if (WindowState == WindowState.Normal)
                WindowState = WindowState.Maximized;
            else
                WindowState = WindowState.Normal;
        }

        public void SetCaption(String caption)
        {
            tbCaption.Text = caption;

             if(!isLoad) return;

            int loop = 1000;//大概计算滚动条总长度，和字符多少有关
            int time = loop / 50;//滚动时间控制（滚动速度控制
            Storyboard Mar_Storyboard = new Storyboard();
            tbCaption.RenderTransform = new TranslateTransform();

            Mar_Storyboard.Children.Add(GetWidthAnimation(tbCaption.Name, TimeSpan.FromSeconds(0), TimeSpan.FromSeconds(time), 0, loop + bdrCaption.Width));
            Mar_Storyboard.RepeatBehavior = RepeatBehavior.Forever;
            Mar_Storyboard.Begin(this);
        }

        private Timeline GetWidthAnimation(string namescope, TimeSpan beginTime, TimeSpan duration, double? fromX, double? toX)
        {
            DependencyProperty[] XpropertyChain = new DependencyProperty[]
            {
                 Rectangle.WidthProperty
            };
            DoubleAnimation yAnimation = new DoubleAnimation()
            {
                From = fromX,
                To = toX,
                Duration = duration,
                BeginTime = beginTime
            };
            Storyboard.SetTargetName(yAnimation, namescope);
            Storyboard.SetTargetProperty(yAnimation, new PropertyPath("(0)", XpropertyChain));
            return yAnimation;
        }
    }
}
